package com.dlc.shop.user.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.user.common.model.UserRights;
import com.dlc.shop.user.common.service.UserRightsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 权益
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Tag(name = "会员权益")
@RestController
@AllArgsConstructor
@RequestMapping("/user/userRights" )
public class UserRightsController {

    /**
     * 系统核销中最大的权益id
     */
    private final Integer MAX_RIGHTSID_OF_SERVICETYPE = 5;
    private final UserRightsService userRightsService;

    @GetMapping
    @Operation(summary = "权益列表" , description = "权益列表")
    @PreAuthorize("@pms.hasPermission('user:userRights:list')" )
    public ServerResponseEntity<IPage<UserRights>> listUserLevel(PageParam<UserRights> page, UserRights userRights) {
        userRights.setShopId(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success( userRightsService.getRightsPage(page, userRights, I18nMessage.getLang()));
    }

    /**
     * 通过id查询
     * @param rightsId id
     * @return 单个数据
     */
    @GetMapping("/info/{rightsId}" )
    @Parameter(name = "rightsId", description = "权益id" )
    @Operation(summary = "查询信息" , description = "通过id查询信息")
    @PreAuthorize("@pms.hasPermission('user:userRights:info')")
    public ServerResponseEntity<UserRights> getById(@PathVariable("rightsId") Long rightsId) {
        return ServerResponseEntity.success(userRightsService.getRightsById(rightsId));
    }

    @SysLog("新增" )
    @PostMapping
    @Operation(summary = "新增" , description = "新增")
    @PreAuthorize("@pms.hasPermission('user:userRights:save')" )
    public ServerResponseEntity<String> save(@RequestBody @Valid UserRights userRights) {
        userRights.setShopId(Constant.PLATFORM_SHOP_ID);
        // 1:商家线下核销
        userRights.setServiceType(1);
        userRights.setStatus(1);
        userRightsService.savaUserRights(userRights);
        return ServerResponseEntity.success(userRights.getRightsId()+"");
    }

    @SysLog("修改" )
    @PutMapping
    @Operation(summary = "修改" , description = "修改")
    @PreAuthorize("@pms.hasPermission('user:userRights:update')" )
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid UserRights userRights) {
        userRights.setShopId(Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success(userRightsService.updateRights(userRights));
    }

    @SysLog("删除" )
    @DeleteMapping("/{rightsId}" )
    @Operation(summary = "删除" , description = "通过id删除")
    @Parameter(name = "rightsId", description = "权益id" )
    @PreAuthorize("@pms.hasPermission('user:userRights:delete')" )
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long rightsId) {
        return ServerResponseEntity.success(userRightsService.removeRights(rightsId));
    }

    @GetMapping("/list")
    @Operation(summary = "获取权益列表" , description = "获取权益列表")
    @PreAuthorize("@pms.hasPermission('user:userRights:list')")
    public ServerResponseEntity<List<UserRights>> list() {
        return ServerResponseEntity.success(userRightsService.getRightsList(Constant.PLATFORM_SHOP_ID));
    }

    @GetMapping("/getMaxSeq" )
    @Operation(summary = "获取最大的序号" , description = "获取最大的序号")
    @PreAuthorize("@pms.hasPermission('user:userRights:getMaxSeq')")
    public ServerResponseEntity<Integer> getMaxSeq() {
        List<UserRights> list = userRightsService.list(new LambdaQueryWrapper<UserRights>()
                .eq(UserRights::getServiceType, Constant.SERVICE_TYPE)
                .eq(UserRights::getShopId, Constant.PLATFORM_SHOP_ID));
        Integer seq = 1;
        for (UserRights userRights:list){
            if (userRights.getSeq()> seq){
                seq = userRights.getSeq();
            }
        }
        seq++;
        return ServerResponseEntity.success(seq);
    }
}
